#include "world.h"
#include "MonsterInfo.h"
#include "CppSQLite3.h"
#include "tile.h"

#include <iostream>
using std::cerr;

bool MonsterInfo::LoadInfo(const char *dbfile)
{
    try {
        CppSQLite3DB db;
        db.open(dbfile);
        if (!db.tableExists("Monster")) {
            return false;
        }

        MonsterAttr mattr;
        CppSQLite3Query q = db.execQuery("SELECT * FROM Monster ORDER BY name");
        while (!q.eof()) {
            mattr.m_index = q.getIntField(0);
            mattr.m_name  = q.getStringField(1);
            mattr.m_tid    = q.getIntField(2);
            mattr.m_dead_tid    = q.getIntField(3);
            mattr.m_frame = q.getIntField(4);
            mattr.m_hasdeadtile  = q.getIntField(5);
            mattr.m_hp    = q.getIntField(6);

            mattr.m_tile = INST(TileManager).getTile(mattr.m_tid);
            if (mattr.m_hasdeadtile)
                mattr.m_dtile = INST(TileManager).getTile(mattr.m_dead_tid);
            else
                mattr.m_dtile = NULL;

            m_vmonsIndex[mattr.m_index] = m_vmonsattr.size();
            m_vmonsattr.push_back(mattr);

            cerr << "load " << mattr.m_index << " " << mattr.m_name << "\n";

            q.nextRow();
        }
    }
    catch (CppSQLite3Exception &e) {
        return false;
    }

    return true;
}
